﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using EShop.Common;
using EShop.BussinessObjects;
using MySql.Data.MySqlClient;

namespace EShop.DataAccess
{
    public class CDACompanyGalleryItem
    {
        public static List<CompanyGalleryItem> Select(CompanyGalleryItem companyGalleryItem)
        {
            MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            string NotEffective = ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString();
            con.Open();
            string _cmdText =
                            "SELECT c.* " +
                            "FROM companyGalleryItem c " +
                            "WHERE ((c.ID = " + companyGalleryItem.ID.ToString() + ") OR (" + companyGalleryItem.ID.ToString() + " = " + NotEffective + "))" +
                            "AND ((c.CompanyGalleryID=" + companyGalleryItem.CompanyGallery.ID.ToString() + ")OR(" + companyGalleryItem.CompanyGallery.ID.ToString() + "=" + NotEffective + "))" +
                            "ORDER BY c.ID";

            MySqlCommand cmd = new MySqlCommand(_cmdText, con);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);

            List<CompanyGalleryItem> companyGalleryItemList = new List<CompanyGalleryItem>();

            CompanyGalleryItem _companyGalleryItem;
            foreach (DataRow row in tbl.Rows)
            {
                _companyGalleryItem = new CompanyGalleryItem();
                _companyGalleryItem.ID = (row["ID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ID"]));

                _companyGalleryItem.CompanyGallery = new CompanyGallery();
                _companyGalleryItem.CompanyGallery.ID = (row["CompanyGalleryID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["CompanyGalleryID"]));

                _companyGalleryItem.Caption = (row["Caption"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue).ToString() : row["Caption"].ToString());
                _companyGalleryItem.Ref = (row["Ref"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue).ToString() : row["Ref"].ToString());
                _companyGalleryItem.ThumbRef = (row["ThumbRef"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue).ToString() : row["ThumbRef"].ToString());

                companyGalleryItemList.Add(_companyGalleryItem);
            }
            con.Close();
            return companyGalleryItemList;

        }
    }
}